//
//  WJQPhoneLoginViewController.m
//  TripTogether
//
//  Created by dllo on 16/1/3.
//  Copyright © 2016年 杨泓晔. All rights reserved.
//

#import "WJQPhoneLoginViewController.h"

@interface WJQPhoneLoginViewController ()

@property (nonatomic, strong) UITextField *phoneTextF;
@property (nonatomic, strong) UITextField *passTextF;

@property (nonatomic, strong) UIButton *loginBtn;

@end

@implementation WJQPhoneLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createBackButton];
    self.view.backgroundColor = [UIColor colorWithRed:0.320 green:0.740 blue:0.800 alpha:1.000];
    
    [self crateViews];
}
- (void)crateViews
{
    
    
    _phoneTextF = [UITextField new];
   // _phoneTextF.backgroundColor = [UIColor whiteColor];
    _phoneTextF.placeholder = @"请填写手机号码/用户名";
    _phoneTextF.keyboardType = UIKeyboardTypeDefault;
    [self.view addSubview:_phoneTextF];
    _phoneTextF.textColor = [UIColor whiteColor];

  [_phoneTextF setValue:[UIColor colorWithWhite:1.000 alpha:0.500] forKeyPath:@"_placeholderLabel.textColor"];
    [_phoneTextF setValue:[UIFont systemFontOfSize:15.f] forKeyPath:@"_placeholderLabel.font"];
    [_phoneTextF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(200, 30));
        make.left.mas_equalTo(@50);
        make.top.mas_equalTo(@180);
    }];
    
    
    
    UIView *viewline = [UIView new];
    viewline.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:viewline];
    [viewline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_phoneTextF.mas_bottom).offset(8);
        make.left.mas_equalTo(@40);
        make.right.mas_equalTo(self.view).offset(- 40);
        make.height.mas_equalTo(1);
        
    }];
    
    
    
    
    
    
    
    
    _passTextF = [UITextField new];
   // _passTextF.backgroundColor = [UIColor whiteColor];
    _passTextF.placeholder = @"输入登陆密码, 至少6位";
    _passTextF.secureTextEntry = YES;
    [self.view addSubview:_passTextF];
    [_passTextF setValue:[UIColor colorWithWhite:1.000 alpha:0.500] forKeyPath:@"_placeholderLabel.textColor"];
    [_passTextF setValue:[UIFont systemFontOfSize:15.f] forKeyPath:@"_placeholderLabel.font"];
    _passTextF.textColor = [UIColor whiteColor];

    [_passTextF mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(200, 30));
        make.left.mas_equalTo(@50);
        make.top.mas_equalTo(viewline.mas_bottom).offset(20);
    }];
    
    
    UIView *v = [UIView new];
    v.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:v];
    [v mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_passTextF.mas_bottom).offset(6);
        make.left.mas_equalTo(@40);
        make.right.mas_equalTo(self.view).offset(- 40);
        make.height.mas_equalTo(1.5);

        
    }];
    
    
    
    
    _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_loginBtn setTitle:@"立即登陆" forState:UIControlStateNormal];
    [_loginBtn addTarget:self action:@selector(loginBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_loginBtn];
    _loginBtn.layer.borderWidth = 1.5f;
    _loginBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    _loginBtn.layer.cornerRadius = 21.f;
    [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(@0);
        make.size.mas_equalTo(CGSizeMake(180, 40));
        make.top.mas_equalTo(v.mas_bottom).offset(100);
    }];
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [self.view addGestureRecognizer:tap];
    
}
- (void)tapAction
{
    if ([_passTextF isFirstResponder]) {
        [_passTextF resignFirstResponder];
    }
    if ([_phoneTextF isFirstResponder]) {
        [_phoneTextF resignFirstResponder];
    }
}

- (void)loginBtnAction:(UIButton *)sender
{
    
    if ([self checkPassword:_passTextF.text]) {
        [self MBShow];
        
        [BmobUser loginInbackgroundWithAccount:_phoneTextF.text andPassword:_passTextF.text block:^(BmobUser *user, NSError *error) {
            
            if (user) {
                
                // 登陆成功
                
                [self dismissViewControllerAnimated:NO completion:^{
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"loginClose" object:nil];
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"refurbish" object:nil];
                    
                }];

            } else {
                // 登陆失败
                
                if (101 == error.code) {
                    [self alertControllerWithTitle:@"提示" message:@"账号或密码不正确"];
                } else {
                    [self alertControllerWithTitle:@"登陆失败" message:@"请核对账号和密码"];
                }

            }
            [self MBHide];
        }];
    } else {
        [self alertControllerWithTitle:@"错误" message:@"密码或账号有误"];
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
